package it.agm.fanta.adapter;

import it.agm.fanta.R;
import it.agm.fanta.model.LegaSquadra;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class LegaSnippetAdapter extends ArrayAdapter<LegaSquadra> {
	
	public LegaSnippetAdapter(Context context, int textViewResourceId,
			ArrayList<LegaSquadra> objects) {
        super(context, textViewResourceId, objects);
    }

	@Override
    public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.drawer_snippet_item, null); 
        convertView = initView(position,convertView);
        return convertView;
    }

	/**
	 * @param position
	 * @return
	 */
	public View initView(int position,View convertView) {
		
		
        TextView nomeSquadra = (TextView)convertView.findViewById(R.id.text_pirates);
        ImageView imageSquadra = (ImageView) convertView.findViewById(R.id.img_pirates);
        LegaSquadra lega = getItem(position);
        if(lega.getScudetto() != null){
			Bitmap bmp = BitmapFactory.decodeByteArray(lega.getScudetto(),
					0, lega.getScudetto().length);
//			ImageView image = (ImageView) convertView.findViewById(R.id.scudetto);
			imageSquadra.setImageBitmap(bmp);
		}
//        imageSquadra.setImageDrawable(lega.getScudetto());
        nomeSquadra.setText(""+ lega.getNome());
		return convertView;
	}
	@Override
    public View getDropDownView(int position, View convertView,
                                ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.spinner_dropdown_item, null); 
       
        return initView(position,convertView);
    }
	
	

}
